
package com.android.fitnesscenter.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.android.fitnesscenter.view.client.ClientLoginActivity;

public class MainApplication extends Application {
    private List<Activity> activities = new ArrayList<Activity>();

    private static MainApplication myApplication;

    /**
     * 获取当前应用全局的Context，注意使用该方法必须要在AndroidManifest.xml中配置MyApplication
     * 
     * @Title: getAppContext
     * @return Context
     */
    public static Context getAppContext() {
        if (null == myApplication) {
            throw new RuntimeException("请在AndroidManifest.xml中配置MyApplication");
        }
        return myApplication;
    }

    /**
     * 获取当前应用全局的Application，注意使用该方法必须要在AndroidManifest.xml中配置MyApplication
     * 
     * @Title: getAppContext
     * @return Application
     */
    public static Application getApp() {
        if (null == myApplication) {
            throw new RuntimeException("请在AndroidManifest.xml中配置MyApplication");
        }
        return myApplication;
    }

    @Override
    public void onCreate() {
        myApplication = this;
        super.onCreate();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
    }

    public boolean isServiceRun(Context context, String serviceAction) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningServiceInfo> list = am.getRunningServices(30);
        for (RunningServiceInfo info : list) {
            String serviceClass = info.service.getClassName();
            if (TextUtils.equals(serviceAction, serviceClass)) {
                return true;
            }
        }
        return false;
    }

    public void addActivity(Activity activity) {
        activities.add(activity);
        Loger.d(" addActivity " + activity);
    }

    public void removeActivity(Activity activity) {
        try {
            activities.remove(activity);
            Loger.d(" removeActivity " + activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearAllChildActivitys() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public void clearChildClientActivitys() {
        for (Activity activity : activities) {
            if (activity != null) {
                if (activity instanceof ClientLoginActivity) {
                } else {
                    activity.finish();
                }
            }
        }
    }

    public void clearChildServerActivitys() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
            }
        }
    }
}
